package com.pedro.streamer.utils;

import static androidx.constraintlayout.widget.Constraints.TAG;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;


import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FileUtils {
    public static String TAG = "com.bmi.vcam";


    public static Bitmap loadImageFromLocal(String imagePath) {
        try {
            FileInputStream fis = new FileInputStream(imagePath);
            return BitmapFactory.decodeStream(fis);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static List<String> getAllImagePaths(String directory) {
        List<String> imagePaths = new ArrayList<>();
        File dir = new File(directory);
        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isFile() && isImageFile(file)) {
                    imagePaths.add(file.getPath());
                }
            }
        }
        return imagePaths;
    }

    private static boolean isImageFile(File file) {
        String fileName = file.getName();
        return fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".jpeg");
    }



    public static void deleteDirectory(String folderPath) {
        File folder = new File(folderPath);
        // 检查文件夹是否存在，且必须为目录
        if (folder.exists() && folder.isDirectory()) {
            // 获取该目录下的所有文件
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 如果是文件，进行删除
                    if (file.isFile()) {
                        if (file.delete()) {
                            System.out.println("Deleted file: " + file.getName());
                        } else {
                            System.out.println("Failed to delete file: " + file.getName());
                        }
                    }else {
                        deleteDirectory(file.getPath());
                    }
                }
            }
        } else {
            System.out.println("Folder doesn't exist or is not a directory.");
        }


    }

    public static void sortFilesByModifiedTime(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        if (files != null) {
            Arrays.sort(files, new Comparator<File>() {
                @Override
                public int compare(File f1, File f2) {
                    long diff = f1.lastModified() - f2.lastModified();
                    if (diff > 0)
                        return 1;
                    else if (diff == 0)
                        return 0;
                    else
                        return -1;
                }
            });
        }


        for (int i = 0; i < files.length; i++) {
            File cur = files[i];
            Log.d(TAG, "getData: " + cur.getName());
        }
    }


    public static void setVolume(String volume){

        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/VideoVolume/";
        deleteDirectory(filePath);


        File newFile = new File(filePath);
        if (!newFile.exists()) {
            newFile.mkdir();
        }

        File volumeFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/VideoVolume/"+volume+".jpg");
        if (!volumeFile.exists()) {
            try {
                volumeFile.createNewFile();
            } catch (Exception e) {

            }

        }
    }


    public static String getVolume(){
        File newFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/VideoVolume/");
        if (!newFile.exists()) {
            newFile.mkdir();
        }

        if (newFile.isDirectory()) {
            File[] files = newFile.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {

                    File file = files[i];
                    if (file.exists()){

                        String nameStr = file.getName();
                        return nameStr.replace(".jpg","");
                    }

                }

            } else {

            }
        }

        return "100";
    }




    public static ArrayList<String> getVideoArr() {

        ArrayList<String> arr = new ArrayList<>();
        File newFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/VideoManager/");
        Log.d(TAG, "newFile path: "+newFile.getAbsolutePath());
        if (!newFile.exists()) {
            newFile.mkdir();
        }
        if (newFile.isDirectory()) {
            // 获取目录中的文件列表
            File[] files = newFile.listFiles();


            if (files != null) {
                Log.d(TAG, "files.length: "+files.length);
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    if (file.exists()){
                        File[] newFiles = file.listFiles();
                        for (File videoFile:newFiles){
                            Log.d(TAG, "videoFile.name: "+videoFile.getName());
                            arr.add(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/" + videoFile.getName());

                        }
                    }


                }

            } else {

            }
        }

        return arr;
    }


    public static void saveData(ArrayList<String> videoPicArr) {


        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/VideoManager/";
        deleteDirectory(filePath);

        File file = new File(filePath);

        if (!file.exists()) {
            file.mkdir();
        }

        Log.d(TAG, "saveData: ");
        for (int i = 0; i < videoPicArr.size(); i++) {
            File videofold = new File(filePath + i);
            if (!videofold.exists()) {
                    videofold.mkdir();
            }


            File vodeo = new File(videoPicArr.get(i));
            File videoFile = new File(videofold.getPath() +"/"+ vodeo.getName());
            Log.d(TAG, "saveData: "+videofold.getPath() +"/"+ vodeo.getName());
            if (!videoFile.exists()) {
                try {
                    videoFile.createNewFile();
                } catch (Exception e) {

                }

            }

        }


    }
}
